#include "Solution.h"
#include <iostream>

using namespace std;

int main()
{
	int n;
	cin>>n;
	vector<int> num(n);

	for(int i=0;i<n;i++)
		cin>>num[i];

	Solution s;
	s.nextPermutation(num);

	for(size_t i=0;i<num.size();i++)
	{
		cout<<num[i]<<" ";
	}

	while(cin.get() != '\n');
	cin.get();

}